//
//  TimerPickerCell.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/10/25.
//

import UIKit
import SnapKit

let kTimerPickerCell: String = "TimerPickerCell"

class TimerPickerCell: RoundCornerCell {
    
    private var datas = Array<Int>()
    
    var model: TimerPickerCellModel? {
        didSet {
            if let minute = model?.duration {
                if minute <= 0 {
                    model?.pickerViewResult?(1)
                    timerPickerView.selectRow(0, inComponent: 0, animated: true)
                } else {
                    model?.pickerViewResult?(minute)
                    timerPickerView.selectRow(minute - 1, inComponent: 0, animated: true)
                }
            } else {
                model?.pickerViewResult?(60) // 默认60分钟
                timerPickerView.selectRow(59, inComponent: 0, animated: true)
            }
        }
    }

    @IBOutlet weak var timerPickerView: UIPickerView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        timerPickerView.backgroundColor = .systemBackground
        timerPickerView.delegate = self
        timerPickerView.dataSource = self
        
        for i in 1...1440 {
            datas.append(i)
        }
        
        // 默认60分钟
        timerPickerView.selectRow(59, inComponent: 0, animated: true)
        
        if #available(iOS 14.0, *) {
            timerPickerView.subviews[1].backgroundColor = .clear
        }
    }
    
}

extension TimerPickerCell: UIPickerViewDelegate, UIPickerViewDataSource {
    // 列数
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return datas.count
        }
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 35
    }
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return 80
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let view = TimerPickerTitleView()
        if component == 0 {
            view.label.text = String(format: "%02d", datas[row])
        } else {
            view.label.text = "分"
        }
        return view
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        model?.pickerViewResult?(datas[row])
    }
}


class TimerPickerTitleView: UIView {
    var label: UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: .zero)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        label = UILabel()
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 17)
        addSubview(label)
        label.snp.makeConstraints { make in
            make.left.right.top.bottom.equalTo(self)
        }
    }
}
